package Init

import (
	"MySQL_BackUp/service"
	"MySQL_BackUp/utils"
	"database/sql"
	"fmt"
	"log"
	"time"
)

// Init 函数用于初始化服务，包括检查数据库连接、yaml模板文件加载
func Init(yamlFilePath string) (*service.MySQLBackupService, error) {
	// 加载基本配置(拿到yaml文件中的相关配置)
	config, err := utils.LoadConfig(yamlFilePath, nil)
	/*
		fmt.Printf("第一次加载配置文件 %v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v", config.BackupPath, config.BinlogFile, config.BinlogPath, config.CurrentDay, config.ExpiredDay, config.Host,
			config.Innobackupex, config.LogFile, config.LogPath, config.MyCnf, config.MySQLPort, config.MySock, config.Password,
			config.Username, config.Xtrabackup)
		第一次加载配置文件 /export/servers/data/mybackup/my<no value>/xtrabackup/data,full_backup_binlog_<no value>.log,/export/servers/data/mybackup/my<no value>/xtrabackup/binlog,<no value>,<no value>,192.168.56.131,/usr/bin/innobackupex,full_backup_data_<no value>.log,/export/servers/data/mybackup/my<no value>/xtrabackup/log,/export/servers/data/my<no value>/my.cnf,3306,/export/servers/data/my<no value>/run/mysqld.sock,srdssttplCDYbdh98JkfyFVfEJBDgLQU,dbbackup,/usr/bin/xtrabackup
	*/
	if err != nil {
		log.Fatalf("failed to load config: %v", err)
	}

	/*
		给yaml文件中的三个模板变量赋值
	*/
	// 获取当前日期
	currentDay := time.Now().Format("2006-01-02")
	// 假设过期日期是当前日期后7天
	expiredDay := time.Now().AddDate(0, 0, 7).Format("2006-01-09")
	data := map[string]string{
		"current_day": currentDay,
		"expired_day": expiredDay,
		"mysql_port":  config.MySQLPort,
	}

	// 再次加载配置，替换掉yaml中的模板变量 {{ . xxx }}
	finalConfig, err := utils.LoadConfig(yamlFilePath, data)
	/*
		fmt.Printf("第二次加载配置文件 %v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v,%v", finalConfig.BackupPath, finalConfig.BinlogFile, finalConfig.BinlogPath, finalConfig.CurrentDay, finalConfig.ExpiredDay, finalConfig.Host,
			finalConfig.Innobackupex, finalConfig.LogFile, finalConfig.LogPath, finalConfig.MyCnf, finalConfig.MySQLPort, finalConfig.MySock, finalConfig.Password,
			finalConfig.Username, finalConfig.Xtrabackup)
		第二次加载配置文件 /export/servers/data/mybackup/my3306/xtrabackup/data,full_backup_binlog_2024-06-18.log,/export/servers/data/mybackup/my3306/xtrabackup/binlog,2024-06-18,2024-06-09,192.168.56.131,/usr/bin/innobackupex,full_backup_data_2024-06-18.log,/export/servers/data/mybackup/my3306/xtrabackup/log,/export/servers/data/my3306/my.cnf,3306,/export/servers/data/my3306/run/mysqld.sock,srdssttplCDYbdh98JkfyFVfEJBDgLQU,dbbackup,/usr/bin/xtrabackup
	*/
	if err != nil {
		log.Fatalf("failed to open database connection: %v", err)
	}

	/*
		1、检查数据库连接--连接数据库自带的mysql库验证即可
		2、创建数据库对象
		3、进行ping操作，验证数据库通信是否正常
	*/
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
		finalConfig.Username, finalConfig.Password, finalConfig.Host, finalConfig.MySQLPort, "mysql")
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatalf("failed to open database connection: %v", err)
	}

	if err = db.Ping(); err != nil {
		log.Fatalf("failed to ping database: %v", err)
	}
	log.Printf("mysql connect success: %v", dsn)

	// 初始化 MySQLBackupService 实例，并返回该对象
	backupService := &service.MySQLBackupService{Config: finalConfig, Db: db}

	return backupService, nil
}
